home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / LIB / SSLEEP.C < prev    next >
C/C++ Source or Header  |  1993-09-26  |  18KB  |  515 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s s l e e p . c                                                 */
  3. /*                                                                    */
  4. /*    Smart sleep routines for UUPC/extended                          */
  5. /*                                                                    */
  6. /*    Written by Dave Watt, modified by Drew Derbyshire               */
  7. /*                                                                    */
  8. /*    Generates DOS specific code with Windows support by default,    */
  9. /*    generates call to OS/2 family API if FAMILYAPI is defined       */
  10. /*    generates calls to Windows/NT if WIN32 is defined               */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. /*--------------------------------------------------------------------*/
  14. /*                          RCS Information                           */
  15. /*--------------------------------------------------------------------*/
  16.  
  17. /*
  18.  *    $Id: ssleep.c 1.10 1993/09/27 02:42:11 ahd Exp $
  19.  *
  20.  *    Revision history:
  21.  *    $Log: ssleep.c $
  22.  *     Revision 1.10  1993/09/27  02:42:11  ahd
  23.  *     Use signed number for delay computations under DOS
  24.  *
  25.  *     Revision 1.9  1993/09/24  03:43:27  ahd
  26.  *     Use OS/2 error messages
  27.  *
  28.  *     Revision 1.8  1993/09/20  04:39:51  ahd
  29.  *     OS/2 2.x support
  30.  *
  31.  *     Revision 1.7  1993/08/02  03:24:59  ahd
  32.  *     Further changes in support of Robert Denny's Windows 3.x support
  33.  *
  34.  *     Revision 1.6  1993/07/31  16:22:16  ahd
  35.  *     Changes in support of Robert Denny's Windows 3.x support
  36.  *
  37.  *     Revision 1.5  1993/07/22  23:19:50  ahd
  38.  *     First pass for Robert Denny's Windows 3.x support changes
  39.  *
  40.  *     Revision 1.4  1993/04/11  00:32:29  ahd
  41.  *     Global edits for year, TEXT, etc.
  42.  *
  43.  * Revision 1.3  1992/12/12  16:12:13  ahd
  44.  * Correct test for DesqView
  45.  *
  46.  * Revision 1.2  1992/12/07  02:43:20  ahd
  47.  * Add DesqView support from David M. Watt
  48.  *
  49.  * Revision 1.1  1992/11/16  05:00:26  ahd
  50.  * Initial revision
  51.  *
  52.  */
  53.  
  54. /*--------------------------------------------------------------------*/
  55. /*                        System include files                        */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. #include <limits.h>
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <signal.h>
  62. #include <time.h>
  63.  
  64. /*--------------------------------------------------------------------*/
  65. /*               MS-DOS and OS/2 specific include files               */
  66. /*--------------------------------------------------------------------*/
  67.  
  68. #if defined(FAMILYAPI) || defined(__OS2__)
  69.  
  70. #define INCL_NOPM
  71. #define INCL_BASE
  72. #include <os2.h>
  73.  
  74. #else
  75.  
  76. #include <dos.h>
  77. #include <sys/timeb.h>
  78.  
  79. #endif
  80.  
  81. #if defined(WIN32) || defined(_Windows)
  82. #include <windows.h>
  83. #endif
  84.  
  85. /*--------------------------------------------------------------------*/
  86. /*                    UUPC/extended include files                     */
  87. /*--------------------------------------------------------------------*/
  88.  
  89. #include "lib.h"
  90. #include "ssleep.h"
  91. #include "safeio.h"
  92. #include "catcher.h"
  93.  
  94. #if defined(_Windows)
  95. #include "winutil.h"
  96. #endif
  97.  
  98.  
  99. #if defined(FAMILYAPI) || defined(__OS2__)
  100. #include "pos2err.h"
  101. #endif
  102.  
  103. /*--------------------------------------------------------------------*/
  104. /*                          Global variables                          */
  105. /*--------------------------------------------------------------------*/
  106.  
  107. #if defined(FAMILYAPI) || defined(__OS2__)
  108. currentfile();
  109. #endif
  110.  
  111.  
  112. /*--------------------------------------------------------------------*/
  113. /*                     MS-DOS specific functions                      */
  114. /*--------------------------------------------------------------------*/
  115.  
  116. #if !defined(FAMILYAPI) && !defined(__OS2__)
  117.  
  118. currentfile();
  119.  
  120. #define MULTIPLEX 0x2F
  121. #define DESQVIEW 0x15
  122.  
  123. #ifdef _Windows
  124.  
  125. static void WindowsDelay( const int milliseconds );
  126.  
  127. /*--------------------------------------------------------------------*/
  128. /*    W i n d o w s D e l a y                                         */
  129. /*                                                                    */
  130. /*    Delay processing under Windows                                  */
  131. /*                                                                    */
  132. /*    NOTE: Minimum resolution is 54.925 ms.                          */
  133. /*--------------------------------------------------------------------*/
  134.  
  135. static void WindowsDelay( const int milliseconds )
  136. {
  137.    MSG msg;
  138.    WORD TimerId = 1;
  139.    BOOL bTimerDone = FALSE;
  140.  
  141.    //
  142.    //    A 0-delay call means give up control to Windows
  143.    //
  144.  
  145.    if (milliseconds == 0)
  146.    {
  147.       while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  148.       {
  149.          TranslateMessage(&msg);
  150.          DispatchMessage(&msg);
  151.       }
  152.       return;
  153.    }
  154.  
  155.    SetTimer( hOurWindow,
  156.             TimerId,
  157.             (milliseconds > 55) ? (WORD)milliseconds : (WORD)55 ,
  158.             NULL );
  159.  
  160.    if ( TimerId == 0 )
  161.    {
  162.       printmsg(0, "WindowsDelay: Unable to set Windows Timer");
  163.       panic();
  164.       return;
  165.    } /* if */
  166.  
  167.    //
  168.    // LOCAL MESSAGE LOOP - Service Windows while waiting for
  169.    // the timer message.
  170.    //
  171.  
  172.    while(!bTimerDone && GetMessage(&msg, NULL, NULL, NULL))
  173.    {
  174.       TranslateMessage(&msg);
  175.       DispatchMessage(&msg);
  176.       if (msg.message == WM_TIMER)
  177.          bTimerDone = TRUE;
  178.    }
  179.  
  180.    if (KillTimer( hOurWindow, TimerId ) == 0)
  181.       printmsg(0, "WindowsDelay: Unable to kill Windows Timer %d",
  182.                   (int) TimerId );
  183.  
  184. } /* WindowsDelay */
  185.  
  186. #else
  187.  
  188. #ifndef WIN32
  189.  
  190. /*--------------------------------------------------------------------*/
  191. /*                      Local function declares                       */
  192. /*--------------------------------------------------------------------*/
  193.  
  194. static void WinGiveUpTimeSlice(void);
  195. static int RunningUnderWindows(void);
  196. static int RunningUnderDesqview(void);
  197. static void DVGiveUpTimeSlice(void);
  198.  
  199. /*--------------------------------------------------------------------*/
  200. /*              Use this first to see if the rest are OK              */
  201. /*                                                                    */
  202. /*                  MOV AX,1600h   ; Check for win386/win3.0          */
  203. /*                                   present                          */
  204. /*                  INT 2Fh                                           */
  205. /* Return AL = 0 -> No Windows, AL = 80 -> No WIn386 mode             */
  206. /*        AL = 1 or AL = FFh -> Win386 2.xx running                   */
  207. /*   else AL = Major version (3), AH = Minor version                  */
  208. /*--------------------------------------------------------------------*/
  209. /* --------------- Release time slice                                 */
  210. /*                  MOV AX,1680h   ; **** Release time slice          */
  211. /*                  INT 2Fh        ; Let someone else run             */
  212. /* Return code is AL = 80H -> service not installed, AL = 0 -> all    */
  213. /*                                                              OK    */
  214. /*--------------------------------------------------------------------*/
  215. /* --------------- Enter critical section (disable task switch)       */
  216. /*                  MOV AX,1681H   ; Don't tread on me!               */
  217. /*                  INT 2Fh                                           */
  218. /*--------------------------------------------------------------------*/
  219. /* --------------- End critical section (Permit task switching)       */
  220. /*                  MOV AX,1682h                                      */
  221. /*                  INT 2Fh                                           */
  222. /*--------------------------------------------------------------------*/
  223.  
  224. /*--------------------------------------------------------------------*/
  225. /*    R u n n i n g U n d e r W i n d o w s                           */
  226. /*                                                                    */
  227. /*    Determines if we are running under M